package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CombinedCostSaveOrUpdateDTO;
import com.zatech.cgnci.project.dto.SolvencyAdequacySaveOrUpDTO;
import com.zatech.cgnci.project.entity.CgnciAccessoryDO;
import com.zatech.cgnci.project.entity.CgnciCombinedCostDO;
import com.zatech.cgnci.project.entity.CgnciSolvencyAdequacyDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciCombinedCostMapper;
import com.zatech.cgnci.project.service.CgnciAccessoryService;
import com.zatech.cgnci.project.service.CgnciCombinedCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.vo.CgnciAccessoryVO;
import com.zatech.cgnci.project.vo.CombinedCostVO;
import com.zatech.cgnci.project.vo.SolvencyAdequacyVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 综合成本率 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-06-05
 */
@Service
public class CgnciCombinedCostServiceImpl extends ServiceImpl<CgnciCombinedCostMapper, CgnciCombinedCostDO> implements CgnciCombinedCostService {

    @Resource
    CgnciAccessoryService accessoryService;

    @Override
    public List<CombinedCostVO> listCombined(String annual) {
        List<CgnciCombinedCostDO> combinedCostDOS = this.baseMapper.selectList(new LambdaQueryWrapper<CgnciCombinedCostDO>()
                .eq(VerifyUtils.isNotEmpty(annual), CgnciCombinedCostDO::getAnnual, annual));
        if (VerifyUtils.isEmpty(combinedCostDOS)) {
            return null;
        }
        List<CombinedCostVO> combinedCostVOS = ConvertUtils.convert(combinedCostDOS, CombinedCostVO.class);
        combinedCostVOS.forEach(combinedCostVO -> {
            List<CgnciAccessoryDO> accessoryDOList = accessoryService.list(new LambdaQueryWrapper<CgnciAccessoryDO>().eq(CgnciAccessoryDO::getBusinessId, combinedCostVO.getId()));
            if (VerifyUtils.isNotEmpty(accessoryDOList)) {
                List<CgnciAccessoryVO> accessoryVOList = ConvertUtils.convert(accessoryDOList, CgnciAccessoryVO.class);
                combinedCostVO.setAccessoryVOList(accessoryVOList);
            }
        });
        return combinedCostVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveOrUpdateCombined(CombinedCostSaveOrUpdateDTO saveOrUpDTO) {
        UserEntity user = UserContext.getUser();
        CgnciCombinedCostDO combinedCostDO = ConvertUtils.convert(saveOrUpDTO, CgnciCombinedCostDO.class);
        if (saveOrUpDTO.getId() != null) {
            combinedCostDO.setUpdateUserName(user.getNameAndCode());
        } else {
            combinedCostDO.setCreateUserName(user.getNameAndCode());
        }
        this.saveOrUpdate(combinedCostDO);

        // 文件信息
        List<CombinedCostSaveOrUpdateDTO.AccessoryDTO> accessoryDTOList = saveOrUpDTO.getAccessoryDTOList();
        if (VerifyUtils.isNotEmpty(accessoryDTOList)) {
            List<CgnciAccessoryDO> accessoryList = new ArrayList<>();
            accessoryDTOList.forEach(accessory -> {
                CgnciAccessoryDO accessoryDO = accessoryService.getById(accessory.getAccId());
                // 文件绑定业务数据
                accessoryDO.setBusinessId(combinedCostDO.getId());
                accessoryList.add(accessoryDO);
            });
            accessoryService.saveOrUpdateBatch(accessoryList);
        }
        return combinedCostDO.getId();
    }

}
